﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Guestbook
{
    public partial class GuestbookForm : Form
    {
        public GuestbookForm()
        {
            InitializeComponent();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtName.Text = string.Empty;
            txtComment.Text = string.Empty;
            txtEmail.Text = string.Empty;
            btnSave.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtName.Text))
            {
                var book = new GuestbookData();

                if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "GuestBook.xml"))
                {
                    book.ReadXml(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "GuestBook.xml", XmlReadMode.IgnoreSchema);
                }

                var row = book.Guest.AddGuestRow(Guid.NewGuid(), txtName.Text, txtEmail.Text, txtComment.Text,
                                                 DateTime.Now);

                //book.Guest.Rows.Add(row);

                book.WriteXml(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "GuestBook.xml", XmlWriteMode.WriteSchema);

                btnReset_Click(sender, e);
            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            btnSave.Enabled = !string.IsNullOrWhiteSpace(txtName.Text);
        }
    }
}
